package com.example.demo.algorithm.delay_queue;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class Order implements Delayed {
    //设置延时时间
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private long delayTime;

    private String name;

    public Order(String name, long delayTime,TimeUnit timeUnit) {
        this.delayTime = System.currentTimeMillis() + (delayTime > 0 ? timeUnit.toMillis(delayTime) : 0);
        this.name = name;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return delayTime - System.currentTimeMillis();
    }

    //负责对队列中的元素进行排序
    @Override
    public int compareTo(Delayed o) {
        Order order = (Order) o;
        long diff = this.delayTime - order.delayTime;
        if(diff < 0){
            return -1;
        }else{
            return 1;
        }
    }
}
